package com.stylefeng.guns.modular.front.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.common.persistence.model.Department;
import com.stylefeng.guns.core.base.controller.FrontBaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.exception.FrontException;
import com.stylefeng.guns.modular.flagglory.service.IDepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/3/2
 * Time: 11:09
 */
@RestController
@RequestMapping("/front/dept")
public class FrontDeptController extends FrontBaseController {
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping(value = "/getDeptList", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有部门信息", notes = "获取所有部门信息")
    public Object getDeptList() throws FrontException {
        JSONObject result = new JSONObject();
        JSONArray deptArray = new JSONArray();
        List<Department> departments = departmentService.selectList(null);
        for (Department department : departments) {
            if(department.getPid().equals(0)){
                JSONObject departmentJson = (JSONObject) JSONObject.toJSON(department);
                List<Department> children = new ArrayList<>();
                for (Department department_ : departments) {
                    if(department_.getPid().equals(department.getId())){
                        children.add(department_);
                    }
                }
                departmentJson.put("children",children);
                deptArray.add(departmentJson);
            }
        }
        result.put("depts",deptArray);
        return new SuccessTip(result);
    }
}
